<template>
  <el-card shadow="never" class="top-card">
    <el-button class="mb20" @click="handleBack">返回</el-button>
    <el-card shadow="never" class="mb20" v-if="many">
      <div slot="header">批量考评人员</div>
      <span>{{ peopleText }}</span>
    </el-card>
    <el-card v-else shadow="never" class="mb20">
      <div slot="header">
        <span>基本信息</span>
      </div>
      <el-form :model="form" ref="form" :inline="true">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="考评周期：" class="mb0">{{ row.moralityCycleName }}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="科室：" class="mb0">{{ row.deptName }}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="人员：" class="mb0">{{ row.name }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <create-table
      @notSubmit="handleNotSubmit"
      @countGrade="handleCountGrade"
      @postData="handlePostData"
      ref="createTable"
    />
    <subtract-items v-if="addItems.length" ref="subtractItems" @postData="handlePostData" />
    <self-comp
      v-if="!this.many"
      @postData="handlePostData"
      @notSubmit="handleNotSubmit"
      ref="selfComp"
    />
    <!-- 下方条件判断是否存在 
      个人自评提交完显示， 
      科室考评完了，党支部批量考评不显示，单个考评显示
    -->
    <template
      v-if="row.selfCommitTime && (row.deptCommitTime ? (!many && row.deptCommitTime) : true )"
    >
      <office-comp @postData="handlePostData" @notSubmit="handleNotSubmit" ref="officeComp" />
    </template>
    <template
      v-if="row.selfCommitTime && row.deptCommitTime && row.branchStep  && (row.branchCommitTime ? (!many && row.branchCommitTime) : true )"
    >
      <branch-comp @postData="handlePostData" @notSubmit="handleNotSubmit" ref="branchComp" />
    </template>
    <template
      v-if="row.selfCommitTime && row.deptCommitTime && ( row.branchStep ? row.branchCommitTime : true ) "
    >
      <committee-comp @postData="handlePostData" @notSubmit="handleNotSubmit" ref="committeeComp" />
    </template>
    <div class="mt20 tl-center">
      <!-- <el-button @click="handleSave">保存</el-button> -->
      <el-button v-if="type === 'edit'" type="primary" @click="handleSubmit">提交</el-button>
    </div>
  </el-card>
</template>

<script>
import CreateTable from './CreateTable'
import SelfComp from './SelfComp'
import OfficeComp from './OfficeComp'
import BranchComp from './BranchComp'
import CommitteeComp from './CommitteeComp'
import SubtractItems from './subtractItems'

import { getSession, setSession, removeSession, openNotification } from '@/utils/util'
import { noteUpdate, deptExamineCommit, deptExamineListCommit } from '@/api/morality'

export default {
  components: {
    CreateTable,
    SelfComp,
    OfficeComp,
    BranchComp,
    CommitteeComp,
    SubtractItems
  },
  data() {
    return {
      name: null,
      form: {},
      param: {
        moralityNote: {},
        moralityNoteSelfList: []
      },
      addItems: [], // 加减分项目的数组
      type: this.$route.params.type || getSession('createEvaluationType'),
      row: this.$route.params.row || getSession('createEvaluationRow'),
      from: this.$route.params.from || getSession('createEvaluationFrom'),
      many: this.$route.params.many || getSession('createEvaluationMany'),
      first: true,
      second: true,
      third: true,
      fourth: true,
      fifth: true,
      peopleArr: [],
      peopleText: null
    }
  },
  methods: {
    handleBack() {
      if (this.from === 'person') {
        this.$router.push({ name: 'medicalPerson' })
      } else if (this.from === 'office') {
        this.$router.push({ name: 'medicalOffice' })
      } else if (this.from === 'branch') {
        this.$router.push({ name: 'medicalBranch' })
      } else {
        this.$router.push({ name: 'medicalCommittee' })
      }
    },
    // 必填项未填
    handleNotSubmit(num) {
      console.log('notSubmit', num)

      if (num === 1) {
        this.first = false
      } else if (num === 2) {
        this.second = false
      } else if (num === 3) {
        this.third = false
      } else if (num === 4) {
        this.fourth = false
      } else {
        this.fifth = false
      }
      this.$message.error('请注意填写页面的必填项')
    },
    handleCountGrade(arr) {
      this.$nextTick(() => {
        this.$refs.selfComp.countGrade(arr)
      })
    },
    handlePostData(num, obj) {
      console.log('postData', num, obj)
      if (num === 1) {
        this.first = true
        this.$set(this.param, 'moralityNoteSelfList', obj)
      } else if (num === 2) {
        this.second = true
        // this.$set(this.param, 'moralityNote', obj)
        this.param.moralityNote = Object.assign(this.param.moralityNote, obj)
      } else if (num === 3) {
        // 科室传值
        this.third = true
        this.param.moralityNote = Object.assign(this.param.moralityNote, obj)
        // this.$set(this.param, 'moralityNote', obj)
      } else if (num === 4) {
        // 党支部传值
        this.fourth = true
        // this.$set(this.param, 'moralityNote', obj)
        this.param.moralityNote = Object.assign(this.param.moralityNote, obj)
      } else {
        // 纪委传值
        this.fifth = true
        // this.$set(this.param, 'moralityNote', obj)
        this.param.moralityNote = Object.assign(this.param.moralityNote, obj)
      }
      this.$set(this.param.moralityNote, 'moralityNoteId', this.row.moralityNoteId)

      console.log('param', this.param)
    },
    // 提交
    async handleSubmit() {
      this.$refs.createTable.getData()
      if (!this.many) {
        this.$refs.selfComp.getData()
        if (this.addItems.length > 0) {
          // 有加减分项目
          let rewardScore = 0
          this.addItems.map(v => {
            rewardScore += v.rewardValue
          })

          this.$set(this.param.moralityNote, 'rewardScore', rewardScore)
        }
      }

      // 科室审核，
      // 个人自评提交完显示，
      // 科室考评完了，党支部批量考评不显示，单个考评显示
      if (this.row.selfCommitTime) {
        if (this.row.deptCommitTime) {
          if (!this.many) {
            this.$refs.officeComp.getData()
          }
        } else {
          this.$refs.officeComp.getData()
        }
      }
      if (this.row.deptCommitTime) {
        if (this.row.branchCommitTime) {
          // 党支部考评完
          if (!this.many) {
            // 党支部考评完，不是批量才回显
            if (this.row.branchStep) {
              // 如果有党支部环节
              this.$refs.branchComp.getData()
            }
          }
        } else {
          if (this.row.branchStep) {
            // 如果有党支部环节
            this.$refs.branchComp.getData()
          }
        }
      }
      if (this.row.deptCommitTime && !this.row.branchStep) {
        // 有科室考评事件且无党支部考评环节
        this.$refs.committeeComp.getData()
      } else {
        if (this.row.branchCommitTime) {
          this.$refs.committeeComp.getData()
        }
      }

      console.log('first', this.first)

      if (this.first && this.second && this.third && this.fourth && this.fifth) {
        let res = null
        if (!this.many) {
          // 单个审核
          if (this.from === 'person') {
            res = await noteUpdate(this.param)
          } else {
            res = await deptExamineCommit(this.param)
          }
        } else {
          // 批量审核
          let arr = getSession('moralityPeopleArr')
          delete this.param.moralityNote.moralityTargetId
          delete this.param.moralityNote.moralityNoteId
          delete this.param.moralityNote.moralityPlanId
          arr.map(v => {
            v = Object.assign(v, this.param.moralityNote)
            // v.
          })
          res = await deptExamineListCommit(arr)
        }

        console.log('res', res)
        if (res.state === '000000') {
          openNotification('success', '提交操作成功')
          this.handleBack()
        }
      } else {
        if (!this.first) {
          this.$message.error('存在自评分分数未填')
        } else if (!this.second) {
          this.$message.error('自评内容未填写完整')
        }
      }
    },
    // 保存
    handleSave() {
      this.$router.push({
        name: 'medicalPerson'
      })
    }
  },
  created() {
    console.log('createPerson 编辑', this.row, this.type, this.many)

    if (this.row) {
      setSession('createEvaluationRow', this.row)
      setSession('createEvaluationType', this.type)
      setSession('createEvaluationFrom', this.from)
      if (this.$route.params.many) {
        let arr = getSession('moralityPeopleArr')
        this.peopleArr = [...arr]
        let arr1 = []
        arr.map(v => {
          arr1.push(v.name)
        })
        this.peopleText = arr1.join('、')
        console.log('peopleArr', this.peopleArr)

        setSession('createEvaluationMany', this.$route.params.many)
      }

      let newArr = []
      this.addItems = [] // 每次进来要置空一下
      let arr = this.row.moralityNoteSelfVOList

      let arr1 = this.row.map.moralityNoteSelfVO // 基础项目
      let arr2 = this.row.map.evalItemVOS // 加减分项目
      this.addItems = this.row.map.evalItemVOS

      arr1.map(v => {
        if (v.moralityNoteSelfVOList && v.moralityNoteSelfVOList.length > 0) {
          // newArr.push(v)
          // if (v.moralityNoteSelfVOList.length > 1) {
          // 子类数组长度大于1的，将长度赋值给第一项，其他项给个 parentName
          v.moralityNoteSelfVOList[0].count = v.moralityNoteSelfVOList.length
          v.moralityNoteSelfVOList.map(a => {
            a.parentName = v.name
          })
          // }
          newArr = newArr.concat(v.moralityNoteSelfVOList)
        } else {
          newArr.push(v)
        }
      })
      console.log('newArr', newArr)

      // const { selfScore, selfLevel, learningSituation, selfComment } = this.row

      this.$nextTick(() => {
        this.$refs.createTable.edit(newArr)
        if (arr2.length > 0) {
          this.$refs.subtractItems.edit(arr2)
        }
        if (!this.many) {
          // 多人审核的时候，没有自我评价
          this.$refs.selfComp.edit(this.row)
        }
        if (this.row.selfCommitTime) {
          if (this.row.deptCommitTime) {
            // 科室考评完
            if (!this.many) {
              // 科室考评完，不是批量才回显
              this.$refs.officeComp.edit(this.row)
            }
          } else {
            this.$refs.officeComp.edit(this.row)
          }
        }
        if (this.row.selfCommitTime && this.row.deptCommitTime) {
          if (this.row.branchCommitTime) {
            // 党支部考评完
            if (!this.many) {
              // 党支部考评完，不是批量才回显
              if (this.row.branchStep) {
                // 如果有党支部环节
                this.$refs.branchComp.edit(this.row)
              }
            }
          } else {
            if (this.row.branchStep) {
              this.$refs.branchComp.edit(this.row)
            }
          }
        }
        // 考评委员会回显
        if (this.row.selfCommitTime && this.row.deptCommitTime) {
          if (this.row.branchStep && this.row.branchCommitTime) {
            this.$refs.committeeComp.edit(this.row)
          } else {
            this.$refs.committeeComp.edit(this.row)
          }
        }
        // if (this.row.selfCommitTime && this.row.deptCommitTime && this.row.branchCommitTime) {
        //   this.$refs.committeeComp.edit(this.row)
        // }
      })
    }
  },
  destroyed() {
    removeSession('createEvaluationType')
    removeSession('createEvaluationRow')
    removeSession('createEvaluationFrom')
    removeSession('createEvaluationMany')
    removeSession('moralityPeopleArr')
  }
}
</script>

<style scoped>
.top-card >>> .el-textarea.is-disabled .el-textarea__inner {
  background-color: #fff;
  color: #333;
  cursor: default;
}
</style>
